<?php

namespace app\api\service;

use think\facade\Db;

class UserCollectGroupGoodsService
{
    /**
     * 获取收藏商品列表
     * */
    public static function getUserCollectGroupGoodsList($user_id)
    {
        $groupGoodsList = Db::name('user_collect_group_goods')
                            ->field('a.group_goods_id,b.goods_name,b.main_image,b.sell_price,b.group_price')
                            ->alias('a')
                            ->join('group_goods b','a.group_goods_id=b.id')
                            ->withAttr('main_image',function ($value){
                                return request()->domain() . $value;
                            })->where('a.user_id',$user_id)
                            ->order('a.id','desc')
                            ->paginate(10);

        return $groupGoodsList;
    }


    /**
     * 收藏商品
     * */
    public static function saveUserCollectGroupGoods($user_id,$group_goods_id)
    {
        $result =  Db::name('user_collect_group_goods')->insert([
            'user_id' => $user_id,
            'group_goods_id' => $group_goods_id
        ]);

        return $result;
    }


    /**
     * 删除收藏商品
     * */
    public static function deleteUserCollectGroupGoods($user_id,$param)
    {
        $ids = array_column($param,'group_goods_id');

        $result = Db::name('user_collect_group_goods')->where('user_id',$user_id)->whereIn('group_goods_id',$ids)->delete();

        return $result;
    }
}